package main

import (
	"fmt"
	"github.com/fsnotify/fsnotify"
	"log"
	"time"
)

type TestReport struct {
	Id        int8
	FileName  string
	CreatedAt time.Time
	UpdatedAt time.Time
}

func main() {

	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	done := make(chan bool)
	var count int8 = 0
	go func() {
		for {
			select {
			case event := <-watcher.Events:
				//log.Println("event:", event)
				//if event.Op & fsnotify.Write == fsnotify.Write {
				//	log.Println("modified file:", event.Name)
				//}

				fmt.Println("Name：", event.Name)
				fmt.Println("Op：", event.Op)

				if event.Op.String() == "CREATE" {
					count++

					if count%2 == 0 {
						var tr = TestReport{
							Id:        count / 2,
							FileName:  event.Name,
							CreatedAt: time.Now(),
							UpdatedAt: time.Now(),
						}
						fmt.Println(tr)
					}
				}

				//nginx()

				fmt.Println("-------------------------------------------------------------------------------------------------------")
			case err := <-watcher.Errors:
				log.Println("error:", err)
			}
		}
	}()

	err = watcher.Add("D:/Listening")
	if err != nil {
		log.Fatal(err)
	}

	//err = watcher.Add("/path/to/file2") //也可以监听文件夹
	//if err != nil {
	//	log.Fatal(err)
	//}

	<-done

}

//func nginx() {
//	cmd := exec.Command("/usr/local/bin/lunchy", "restart", "nginx") //重启命令根据自己的需要自行调整
//	cmd.Run()
//}
